/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.web.font;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.utils.I18nUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.source.entity.font.EleSourceFont;
import com.thinkgem.jeesite.modules.source.entity.video.EleSourceVideoGroup;
import com.thinkgem.jeesite.modules.source.service.font.EleSourceFontService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 字体文件管理Controller
 * @author ZC
 * @version 2017-07-03
 */
@Controller
@RequestMapping(value = "${adminPath}/source/font/eleSourceFont")
public class EleSourceFontController extends BaseController {

	@Autowired
	private EleSourceFontService eleSourceFontService;
	@Autowired
	private SystemService systemService;
	@ModelAttribute
	public EleSourceFont get(@RequestParam(required=false) String id) {
		EleSourceFont entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleSourceFontService.get(id);
		}
		if (entity == null){
			entity = new EleSourceFont();
		}
		return entity;
	}
	
	@RequiresPermissions("source:font:eleSourceFont:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleSourceFont eleSourceFont, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		List<User> list = null;
		if(user.isAdmin()) {
			list = systemService.findAllUsers(user);
		}else {
			list = systemService.findAllUsersInCompany(user);
			list.add(UserUtils.getAdminUser());		
		}		
		eleSourceFont.setUsers(list);
		Page<EleSourceFont> page = eleSourceFontService.findPage(new Page<EleSourceFont>(request, response), eleSourceFont); 
		List<EleSourceFont> fonts = page.getList();
		for(EleSourceFont font:fonts) {
			String fileName = StringUtils.replace(font.getFont(), "\\", "/");
			System.out.println(fileName);
			font.setFont(fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length()));
		}
		Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);  
		page.setLocale(locale);
		model.addAttribute("page", page);
		return "modules/source/font/eleSourceFontList";
	}

	@RequiresPermissions("source:font:eleSourceFont:view")
	@RequestMapping(value = "form")
	public String form(EleSourceFont eleSourceFont,HttpServletRequest request, Model model) {
		model.addAttribute("eleSourceFont", eleSourceFont);
		Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);  
		model.addAttribute("language", locale.getLanguage());
		if(locale.getLanguage().equals(Locale.CHINESE.getLanguage())){
			model.addAttribute("langCode", locale.getLanguage()+"-cn");
		}else{
			model.addAttribute("langCode", locale.getLanguage());
		}
		return "modules/source/font/eleSourceFontForm";
	}

	@RequiresPermissions("source:font:eleSourceFont:view")
	@RequestMapping(value = "save")
	public String save(EleSourceFont eleSourceFont,HttpServletRequest request, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eleSourceFont)){
			return form(eleSourceFont,request, model);
		}
		eleSourceFontService.save(eleSourceFont);
		String locale = I18nUtils.getLocaleStr();
		String msg = "保存字体文件成功";
		if(Global.US.equals(locale)){
			msg = "Save the font file successfully!";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/source/font/eleSourceFont/?repage";
	}
	
	@RequiresPermissions("source:font:eleSourceFont:view")
	@RequestMapping(value = "delete")
	public String delete(EleSourceFont eleSourceFont, RedirectAttributes redirectAttributes) {
		eleSourceFontService.delete(eleSourceFont);
		String locale = I18nUtils.getLocaleStr();
		String msg = "删除字体文件成功";
		if(Global.US.equals(locale)){
			msg = "Font resource saved";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/source/font/eleSourceFont/?repage";
	}
	@RequestMapping(value = "treeData")
	@ResponseBody
	public List<Map<String, Object>> treeData(HttpServletRequest request){
		EleSourceFont eleSourceFont = new EleSourceFont();
		List<Map<String, Object>> mapList = Lists.newArrayList();
		User user = UserUtils.getUser();
		List<User> list = null;
		if(user.isAdmin()) {
			list = systemService.findAllUsers(user);
		}else {
			list = systemService.findAllUsersInCompany(user);
			list.add(UserUtils.getAdminUser());
		}
		eleSourceFont.setUsers(list);

		List<EleSourceFont> lists = eleSourceFontService.findList(eleSourceFont);
		for(EleSourceFont e:lists){
			  Map<String, Object> map = Maps.newHashMap();
            map.put("id", e.getId());
            map.put("pId", e.getId());
            map.put("pIds", e.getId());
            map.put("name", e.getName());
            mapList.add(map);
		}
		return mapList;

	}
}